﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

using Winsteps.Validator;

public partial class Manage_proManage : System.Web.UI.Page
{
    BLL.ProfessionalBll bll = new BLL.ProfessionalBll();
    Model.Professional model = new Model.Professional();

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            LoadValidator();

            BindData();
            grd_Professional.SelectedIndex = -1;
        }
    }

    private void BindData()
    {
        DataTable proTable = bll.GetAllProfessional();
        grd_Professional.DataSource = proTable;
        grd_Professional.DataBind();
    }

    protected void grd_Professional_SelectedIndexChanged(object sender, EventArgs e)
    {
        string proId = grd_Professional.Rows[grd_Professional.SelectedIndex].Cells[0].Text.ToString();
        DataTable table = bll.GetProById(proId);
        lb_AddOrUpdate.Text = "编辑专业信息";
        txt_ProId.Enabled = false;
        txt_ProId.Text = table.Rows[0]["proId"].ToString();
        txt_ProName.Text = table.Rows[0]["proName"].ToString();
        txt_ProNum.Text = table.Rows[0]["proNum"].ToString();
        txt_Content.Text = table.Rows[0]["proDescribe"].ToString();
        btn_AddProfessional.Enabled = true;
    }

    protected void btn_AddProfessional_Click(object sender, EventArgs e)
    {
        lb_AddOrUpdate.Text = "新增专业信息";
        grd_Professional.SelectedIndex = -1;
        txt_ProId.Enabled = true;
        txt_ProId.Text = "";
        txt_ProName.Text = "";
        txt_ProNum.Text = "";
        txt_Content.Text = "";
    }
    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        model.proId = txt_ProId.Text.ToString();
        model.proName = txt_ProName.Text.ToString();
        model.proNum = Int32.Parse(txt_ProNum.Text.ToString());
        model.proDescribe = Request.Params["editorValue"].ToString();
        
        if (txt_ProId.Enabled == true)
        {
            //新增专业
            if (bll.Exists(model.proId))
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "s", "alert('专业编号为" + model.proId + "已经存在')", true);
            }
            else
            {
                bll.AddProfessional(model);
            }
        }
        else
        {
            bll.UpdateProfessional(model);
        }

        BindData();
    }

    private void LoadValidator()
    {
        WinVal WinValInstance = new WinVal();
        WinValInstance.SetValidator();

        txt_ProId.Attributes.Add("onblur", "this.className='inputblur'");
        txt_ProId.Attributes.Add("onfocus", "this.className='inputfoucs'");

        txt_ProName.Attributes.Add("onblur", "this.className='inputblur'");
        txt_ProName.Attributes.Add("onfocus", "this.className='inputfoucs'");

        txt_ProNum.Attributes.Add("onblur", "this.className='inputblur'");
        txt_ProNum.Attributes.Add("onfocus", "this.className='inputfoucs'");
    }
}
